import 'package:sqflite/sqflite.dart';

import '../dao/ImGroupDao.dart';
import '../dao/ImUserDao.dart';
import '../pojo/ImPojo.dart';
import 'DbUtil.dart';
import 'Objects.dart';

class ImCacheFactory {

  static ImCacheFactory _instance = new ImCacheFactory();

  final _userMap = new Map<String, ImUser>();
  final _groupMap = new Map<int, ImGroup>();
  final _groupCodeMap = new Map<String, ImGroup>();
  final _hotMessageTotalMap = <int, int>{}; // 热点数据

  static ImCacheFactory getInstance() {
    return _instance;
  }

  // Future<ImUser?> getImUser(Database database, String userId) async {
  //   if (_userMap.containsKey(userId)) return Future.value(_userMap[userId]);
  //   var imUserDao = ImUserDao(database);
  //   var user = await imUserDao.getByUserId(userId);
  //   if (user != null) {
  //     _userMap[userId] = user;
  //     return user;
  //   }
  //
  //   throw new Exception(userId + "用户不存在");
  // }

  Future<ImUser?> getImUser(String userId) async {
    if (_userMap.containsKey(userId)) return Future.value(_userMap[userId]);
    var imUserDao = ImUserDao();
    var user = await imUserDao.getByUserId(userId);
    if (user != null) {
      _userMap[userId] = user;
      return user;
    }

    throw new Exception(userId + "用户不存在");
    // return null;
  }

  // Future<ImGroup?> getImGroup(Database database, String currentUserId, String groupCode) async {
  //   var imUser = await getImUser(database, currentUserId);
  //   var groupKey = '${imUser?.id}_${groupCode}';
  //   if (_groupCodeMap.containsKey(groupKey)) return Future.value(_groupCodeMap[groupKey]);
  //
  //   var imGroupDao = ImGroupDao(database);
  //   try {
  //     var group = await imGroupDao.getByCodeAndUserId(groupCode, currentUserId);
  //     if (group != null) {
  //       _groupCodeMap[groupKey] = group;
  //       var gid = group.id;
  //       if (gid != null) {
  //         _groupMap[gid] = group;
  //       }
  //       return group;
  //     }
  //   } finally {
  //     DbUtil.close(database);
  //   }
  //
  //   return null;
  // }

  Future<ImGroup?> getImGroup(String currentUserId, String groupCode) async {
    var imUser = await getImUser(currentUserId);
    var groupKey = '${imUser?.id}_${groupCode}';
    if (_groupCodeMap.containsKey(groupKey)) return Future.value(_groupCodeMap[groupKey]);

    var imGroupDao = ImGroupDao();
    var group = await imGroupDao.getByCodeAndUserId(groupCode, currentUserId);
    if (group != null) {
      await imGroupDao.loadGroupUsers(group);

      _groupCodeMap[groupKey] = group;
      var gid = group.id;
      if (gid != null) {
        _groupMap[gid] = group;
      }
      return group;
    }

    return null;
  }

  int incHotMessage(int sessionId, int value) {
    var total = getHotMessage(sessionId);
    total = Objects.isNull(total) ? 0  : total;
    total += value;
    _hotMessageTotalMap[sessionId] = total;
    if (sessionId > 0) {
      incHotMessage(0, value);
    }
    return total;
  }

  int getHotMessage(int sessionId) {
    return _hotMessageTotalMap[sessionId] ?? 0;
  }

  void cleanHotMessage(int sessionId) {
    var value = _hotMessageTotalMap[sessionId];
    _hotMessageTotalMap.remove(sessionId);
    if (sessionId > 0) {
      incHotMessage(0, 0- (value??0));
    }
  }

  void setHotMessage(int sessionId, int value) {
    if (value < 0) return;
    var oldValue = _hotMessageTotalMap[sessionId]??0;
    if (oldValue > 0) {
      _hotMessageTotalMap[sessionId] = value;

      if (sessionId > 0) {
        incHotMessage(0, value);
      }
    } else {
      var changeValue = value  - oldValue!;
      _hotMessageTotalMap[sessionId] = value;
      if (sessionId > 0) {
        incHotMessage(0, changeValue);
      }
    }
  }
}